package com.ruoyi.web.controller.hrm;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.CxSelect;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.common.exception.AirCalculateException;
import com.ruoyi.web.controller.common.exception.FuelCalculateException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 休假管理统计
 *
 * @author yangtianli
 * @date 2021-12-04
 */
@Controller
@RequestMapping("/hrm/sysHoliday")
public class SysHolidayController extends BaseController
{
    private String prefix = "hrm/holiday";

    @Autowired
    public ISysHolidayTotalService sysHolidayTotalService;
    @Autowired
    public ISysProjectService projectService;
    @Autowired
    public ISysHolidayDetailService sysHolidayDetailService;

    @GetMapping()
    public String holidayList(ModelMap modelMap)
    {
        List<CxSelect> cxList = new ArrayList<>();
        List<CxSelect>cxSelectList = new ArrayList<>();
        List<SysProject> projects = projectService.selectProjectAll();
        CxSelect cxSelect2 = new CxSelect();
        cxSelect2.setV("");
        cxSelect2.setN("所有");//项目默认所有
        for (SysProject p:projects){
            CxSelect cxSelect = new CxSelect();
            cxSelect.setN(p.getProjectName());
            cxSelect.setV(p.getProjectName());
            cxSelectList.add(cxSelect);
        }
        cxList.add(cxSelect2);
        for (CxSelect cxSelect:cxSelectList){
            cxList.add(cxSelect);
        }//按所有在上 的顺序排序
        modelMap.put("csList", JSON.toJSON(cxList));
        return prefix + "/holiday";
    }
//    @RequiresPermissions("hrm:sysHoliday:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysHolidayTotal sysHolidayTotal){
        startPage();
        List<SysHolidayTotal> list = sysHolidayTotalService.listSysHolidayTotal(sysHolidayTotal);
        return getDataTable(list);
    }


//    @RequiresPermissions("hrm:sysHoliday:calculateValue")
    @Log(title = "休假管理-计算汇总数据", businessType = BusinessType.INSERT)
    @PostMapping("/calculateValue")
    @ResponseBody
    public AjaxResult calculateValue(SysHolidayTotal sysHolidayTotal)
    {
        boolean falg = true;
        List<SysHolidayTotal> list = sysHolidayTotalService.selectSysHolidayTotal(sysHolidayTotal);
        //删除所有total历史数据
        sysHolidayTotalService.deleteHolidayTotal(null);
        int i=0;
        for (SysHolidayTotal sysHolidayTotal1:list){
            i++;
            sysHolidayTotal1.setHolidayUuid(UUID.randomUUID().toString());
            sysHolidayTotal1.setSerialNum(String.valueOf(i));
            falg = sysHolidayTotalService.insertSysHolidayTotal(sysHolidayTotal1)>0?true:false;
            if (!falg) break;
        }
        return toAjax(falg);
    }
    /**
     * 导出
     */
    @Log(title = "休假管理", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("hrm:sysHoliday:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysHolidayTotal sysHolidayTotal,String ids)
    {
        List<SysHolidayTotal> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(ids)){
            String[] holidayUuids = Convert.toStrArray(ids);
            for(String holidayUuid:holidayUuids){
                SysHolidayTotal thisHolidayTotal = sysHolidayTotalService.selectSysHolidayTotalById(holidayUuid);
                list.add(thisHolidayTotal);
            }
        }else {
            list = sysHolidayTotalService.listSysHolidayTotal(sysHolidayTotal);
        }
        ExcelUtil<SysHolidayTotal> util = new ExcelUtil<SysHolidayTotal>(SysHolidayTotal.class);
        return util.exportExcel(list, "休假管理");
    }
    /**
     * 导出明细
     */
    @Log(title = "休假管理", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("hrm:sysHoliday:detailExport")
    @PostMapping("/detailExport")
    @ResponseBody
    public AjaxResult detailExport(SysHolidayDetail sysHolidayDetail,String ids)
    {
        List<SysHolidayDetail> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(ids)){
            String[] holidayUuids = Convert.toStrArray(ids);
            for(String holidayUuid:holidayUuids){
                SysHolidayDetail thisHolidayDetail = sysHolidayDetailService.selectSysHolidayDetail(holidayUuid);
                    list.add(thisHolidayDetail);
                }
        }else {
//            list = sysHolidayDetailService.listSysHolidayDetailAll();
            list = sysHolidayDetailService.listSysHolidayDetail(sysHolidayDetail);

        }
        ExcelUtil<SysHolidayDetail> util = new ExcelUtil<SysHolidayDetail>(SysHolidayDetail.class);
        return util.exportExcel(list, "个人明细");
    }

    /**
     * 导入
     */
    @Log(title = "休假管理", businessType = BusinessType.IMPORT)
//    @RequiresPermissions("hrm:sysHoliday:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        String message="";

            ExcelUtil<SysHolidayDetail> util = new ExcelUtil<SysHolidayDetail>(SysHolidayDetail.class);
            List<SysHolidayDetail> holidayList = util.importExcel(file.getInputStream());
            String res = sysHolidayDetailService.validateDatas(holidayList);
            if (StringUtils.isNotBlank(res)){
                return AjaxResult.error(res);
            }
            String operName = ShiroUtils.getSysUser().getLoginName();
            message = sysHolidayDetailService.importSysHolidayDetail(holidayList,updateSupport,operName);
        return AjaxResult.success(message);
    }
//    @RequiresPermissions("hrm:sysHoliday:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
            ExcelUtil<SysHolidayDetail> util = new ExcelUtil<SysHolidayDetail>(SysHolidayDetail.class);
            return util.importTemplateExcel("休假管理");
    }

    /**
     * 跳转明细展示
     * @param modelMap
     * @return
     */
    @GetMapping("/detailList")
    public String toDetail(ModelMap modelMap) {
        return prefix + "/detailList";
    }

    @PostMapping("/detailList")
    @ResponseBody
    public TableDataInfo detailList(SysHolidayDetail sysHolidayDetail) {
        startPage();
        List<SysHolidayDetail> detailList = sysHolidayDetailService.listSysHolidayDetail(sysHolidayDetail);
        return getDataTable(detailList);
    }
    /**
     * 删除明细
     */
    @Log(title = "休假明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String[] ids)
    {
        return toAjax(sysHolidayDetailService.deleteHolidayDetailByIds(ids));
    }

    /**
     * 新增休假明细
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存休假明细
     */
    @Log(title = "休假明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysHolidayDetail sysHolidayDetail)
    {
        String operName = ShiroUtils.getSysUser().getLoginName();
        return toAjax(sysHolidayDetailService.insertSysHolidayDetail(sysHolidayDetail,operName));
    }

    /**
     * 修改休假明细
     */
    @GetMapping("/edit/{holidayUuid}")
    public String edit(@PathVariable("holidayUuid") String holidayUuid, ModelMap mmap)
    {
        SysHolidayDetail sysHolidayDetail = sysHolidayDetailService.selectSysHolidayDetail(holidayUuid);
        mmap.put("sysHolidayDetail", sysHolidayDetail);
        return prefix + "/edit";
    }

    /**
     * 修改休假明细
     */
//    @RequiresPermissions("hrm:sysHoliday:update")
    @Log(title = "休假明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysHolidayDetail sysHolidayDetail)
    {
        String operName = ShiroUtils.getSysUser().getLoginName();
        return toAjax(sysHolidayDetailService.updateSysHolidayDetail(sysHolidayDetail,operName));
    }
}
